package sia.tp1;

import java.lang.reflect.Field;

public class SimulationParams {

	public enum SelectionMethods { elite, roulette, universal, eliteRoulette, eliteUniversal };
	
	//Default param values if not passed by command line:
	public int initialPop = 5000;
	public int generationCount = 999999999;
	public int selectionCount = 300;
	public double mutationP = 0.05;
	public int maxHeight = 8;
	public double explodeP = 0.8;
	public SelectionMethods selectionMethod = SelectionMethods.roulette;
	public SelectionMethods replaceMethod = SelectionMethods.roulette;
	
	SimulationParams(String[] args) throws NumberFormatException, IllegalArgumentException, IllegalAccessException {
		String split[] = null;
		for (String arg : args) {
			split = arg.split("=");
			if(split.length >= 2) {
				try {
					Field aField = SimulationParams.class.getField(split[0]);
					if(aField.getType() == double.class) {
						aField.setDouble(this, new Double(split[1]));
					} else if(aField.getType() == int.class){
						aField.setInt(this, new Integer(split[1]));
					} else {
						aField.set(this, SelectionMethods.valueOf(split[1]));
					}
						
				} catch (NoSuchFieldException e) {
					System.out.println("No such argument : " + split[0] + ", Using default");
				}
			}
		}
		return;
	}

	@Override
	public String toString() {
		return String.format("initialPop = %d; generationCount = %d; selectionCount = %d; mutationP = %g;" +
				"maxHeight = %d; explodeP = %g",
				initialPop, generationCount, selectionCount, mutationP, maxHeight, explodeP);
	}
}
